<?php

include "../class/database-server.php";  
  
  
$targetFolder = '../img/galfotos'; // Relative to the root
$codigo=$_POST['codigo'];
$foto = $_FILES["foto"];


$verifyToken = md5('unique_salt' . $_POST['timestamp']);

    if (!empty($_FILES)) {

        class Image {
            
            var $uploaddir;
            var $quality = 80;
            var $ext;
            var $dst_r;
            var $img_r;
            var $img_w;
            var $img_h;
            var $output;
            var $data;
            var $datathumb;
            
            function setFile($src = null) {
                $this->ext = strtoupper(pathinfo($src, PATHINFO_EXTENSION));
                if(is_file($src) && ($this->ext == "jpg" OR $this->ext == "jpeg")) {
                    $this->img_r = ImageCreateFromJPEG($src);
                } elseif(is_file($src) && $this->ext == "PNG") {
                    $this->img_r = ImageCreateFromPNG($src);      
                } elseif(is_file($src) && $this->ext == "GIF") {
                    $this->img_r = ImageCreateFromGIF($src);
                }
                $this->img_w = imagesx($this->img_r);
                $this->img_h = imagesy($this->img_r);
            }
            
            function resize($w = 100) {
                $h =  $this->img_h / ($this->img_w / $w);
                $this->dst_r = ImageCreateTrueColor($w, $h);
                imagecopyresampled($this->dst_r, $this->img_r, 0, 0, 0, 0, $w, $h, $this->img_w, $this->img_h);
                $this->img_r = $this->dst_r;
                $this->img_h = $h;
                $this->img_w = $w;
            }
            
            function createFile($output_filename = null) {
                if($this->ext == "jpg" OR $this->ext == "jpeg") {
                    imageJPEG($this->dst_r, $this->uploaddir.$output_filename.'.'.$this->ext, $this->quality);
                } elseif($this->ext == "PNG") {
                    imagePNG($this->dst_r, $this->uploaddir.$output_filename.'.'.$this->ext);
                } elseif($this->ext == "GIF") {
                    imageGIF($this->dst_r, $this->uploaddir.$output_filename.'.'.$this->ext);
                }
                $this->output = $this->uploaddir.$output_filename.'.'.$this->ext;
            }
            
            function setUploadDir($dirname) {
                $this->uploaddir = $dirname;
            }
            
            function flush() {
			$tempFile = $_FILES['Filedata']['tmp_name'];
			$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
			$targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
			$ext = pathinfo($_FILES['Filedata']['name'], PATHINFO_EXTENSION);
                
			imagedestroy($this->dst_r);
			unlink($targetFile);
			imagedestroy($this->img_r);
                
				
				
            }
            
        }
        
		$tempFile = $_FILES['Filedata']['tmp_name'];
		$targetPath = $targetFolder . $_GET['folder'] . '/';
		$targetFile = rtrim($targetPath,'/') . '/' . $_FILES['Filedata']['name'];
		$ext = pathinfo($_FILES['Filedata']['name'], PATHINFO_EXTENSION);


		// Gera um nome único para a imagem para inclusão no banco
		$newFileName = md5($tempFile).'.'.$ext;
		$targetFile =  str_replace('//','/',$targetPath) . $newFileName;//salva extensao uppercas	
        
        move_uploaded_file ($tempFile, $targetFile);
        
        $image = new Image();
        $image->setFile($targetFile);
        $image->setUploadDir($targetPath);
        $image->resize(640);
        $image->createFile(md5($tempFile));
        $image->flush();
	
		
		    /*Grava*/   
    if ( !empty($_POST)) {
        $pdo = Database::connect();
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sql = "INSERT INTO galfotos (codigo,foto) values(?,?)";
        $q = $pdo->prepare($sql);
        $q->execute(array($codigo,$newFileName));
        Database::disconnect();	
    }
    /*Grava*/   
		
    }

?>